package com.youzikj.wx.comm.subscribe;

import java.awt.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.youzikj.wx.comm.subscribe.pojo.WeixinUserInfo;
import com.youzikj.wx.comm.subscribe.pojo.WeixinUserList;
import com.youzikj.wx.comm.util.CommonUtil;

/**
 * 获取关注公众号用户信息
 * @author Administrator
 *
 */
public class SubscribeInfoUtil {
	
	private static Logger log = Logger.getLogger(SubscribeInfoUtil.class);
	
	/**
	 * 获取用户信息
	 */
	public static WeixinUserInfo getUserInfo(String accessToken,String openId){
		WeixinUserInfo weixinUserInfo = null;
		String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID";
		requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken).replace("OPENID", openId);
		//获取用户信息
		JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
		if(null != jsonObject){
			try {
				weixinUserInfo = new WeixinUserInfo();
				weixinUserInfo.setOpenId(jsonObject.getString("openid"));
				weixinUserInfo.setSubscribe(jsonObject.getInt("subscribe"));
				weixinUserInfo.setSubscribeTime(jsonObject.getString("subscribe_time"));
				weixinUserInfo.setNickname(jsonObject.getString("nickname"));
				weixinUserInfo.setSex(jsonObject.getInt("sex"));
				weixinUserInfo.setCountry(jsonObject.getString("country"));
				weixinUserInfo.setProvince(jsonObject.getString("province"));
				weixinUserInfo.setCity(jsonObject.getString("city"));
				weixinUserInfo.setLanguage(jsonObject.getString("language"));
				weixinUserInfo.setHeadImgUrl(jsonObject.getString("headimgurl"));
				
			} catch (Exception e) {
				if(0 == weixinUserInfo.getSubscribe()){
					log.error("用户{}已取消关注"+weixinUserInfo.getOpenId());
				}else{
					int errorCode = jsonObject.getInt("errcode");
					String errorMsg = jsonObject.getString("errmsg");
					log.error("获取用户信息失败 errorCode:"+errorCode+" errmsg{}"+errorMsg);
				}
				
			}
		}
		return weixinUserInfo;
		
	
	}
	
	/**
	 * 获取关注用户列表
	 */
	@SuppressWarnings({ "deprecation", "unchecked" })
	public static WeixinUserList getUserList(String accessToken,String nextOpenId){
		WeixinUserList wx = null;
		
		if(null == nextOpenId){
			nextOpenId = "";
			String requestUrl = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID";
			requestUrl.replace("ACCESS_TOKEN", accessToken).replace("NEXT_OPENID", nextOpenId);
			//获取关注者列表
			JSONObject json = CommonUtil.httpsRequest(requestUrl, "GET", null);
			if(null != json){
				try {
				wx = new WeixinUserList();
				wx.setTotal(json.getInt("total"));
				wx.setCount(json.getInt("count"));
				wx.setNextOpenId(json.getString("next_openid"));
				JSONObject datejson =(JSONObject)json.get("date");
				wx.setOpenIdList(JSONArray.toList(datejson.getJSONArray("openid"),List.class));
				} catch (Exception e) {
					wx = null;
					int errorCode = json.getInt("errcode");
					String errorMsg = json.getString("errmsg");
					log.error("获取用户信息失败 errorCode:"+errorCode+" errmsg{}"+errorMsg);
				}
			}
			
		}
		return wx;
	}
	 
	

}
